layui.use('table', function(){
    var table = layui.table;

    //消息列表
    var messageTable = 
    table.render({
        elem: '#demo'
        ,height: 312
        ,url: '/message/all' //数据接口
        ,toolbar: '#toolbar'
        ,title: '留言数据表'
        ,page: true //开启分页
        ,cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
            ,{field: 'name', title: '用户名', width:180}
            ,{field: 'email', title: '邮箱', width: 180, sort: true}
            ,{field: 'message', title: '留言内容', width: 280, sort: true}
            ,{fixed: 'right', title:'操作', toolbar: '#bar', width:180}
        ]]
    });
    //头工具栏事件
    table.on('toolbar(test)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        //console.log(obj.event);
        switch(obj.event){
            //批量删除
            case 'delAll':
                var data = checkStatus.data;
                layer.confirm('确认删除这'+data.length+'行吗？', function(index){
                    
                    var iaArr = data.map(function(v){return v.id;});
                    publicCurdAjax.source = iaArr.join(',');
                    publicCurdAjax.model = 'message';
                    publicCurdAjax.DelAll();
                    
                    layer.close(index);
                    
                    messageTable.reload();
                    
                });
                break;
            case 'getCheckLength':
                var data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
                break;
            case 'isAll':
                layer.msg(checkStatus.isAll ? '全选': '未全选');
                break;
        };
    });
    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        //console.log(data.id);
        if(obj.event === 'del'){
            layer.confirm('确认删除这一行吗？', function(index){
                publicCurdAjax.source = data.id;
                publicCurdAjax.model = 'message';
                publicCurdAjax.Del();
                obj.del();
                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            layer.prompt({
                formType: 2
                ,value: data.email
            }, function(value, index){
                obj.update({
                    email: value
                });
                layer.close(index);
            });
        } else if(obj.event === 'editStatus')
        {
            layer.msg("已读");
        }
        else if(obj.event === 'getCheckLength')
        {
            layer.confirm('确认删除这几行吗？', function(index){
                // publicCurdAjax.id = data.id;
                // publicCurdAjax.model = 'message';
                // publicCurdAjax.D();
                // obj.del();
                layer.close(index);
            });
        }
    });

});